package org.example.practiceexamples.mapper;

import lombok.extern.slf4j.Slf4j;
import org.example.practiceexamples.dox.Appointment;
import org.example.practiceexamples.dox.Lab;
import org.example.practiceexamples.dto.LabAppointment;
import org.springframework.jdbc.core.ResultSetExtractor;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class LabAppointmentResultSetExtractor implements ResultSetExtractor<LabAppointment> {
    @Override
    public LabAppointment extractData(ResultSet rs) throws SQLException {
        Lab lab = null;
        List<Appointment> appointments = new ArrayList<>();
        while (rs.next()) {
            if(lab == null) {
                lab = Lab.builder()
                        .id(rs.getString("l.id"))
                        .name(rs.getString("name"))
                        .description(rs.getString("description"))
                        .state(rs.getInt("state"))
                        .quantity(rs.getInt("quantity"))
                        .manage(rs.getString("manage"))
                        .build();
            }
            Appointment a = Appointment.builder()
                    .id(rs.getString("a.id"))
                    .course(rs.getString("course"))
                    .teacher(rs.getString("teacher"))
                    .labId(rs.getString("lab_id"))
                    .labName(rs.getString("lab_name"))
                    .week(rs.getInt("week"))
                    .dayofweek(rs.getInt("dayofweek"))
                    .section(rs.getInt("section"))
                    .build();
            appointments.add(a);
        }
        return LabAppointment.builder()
                .lab(lab)
                .Appointments(appointments)
                .build();


    }
}
